import service.*;
import model.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        HallService hallService = new HallService();
        UserService userService = new UserService();
        MovieService movieService = new MovieService();
        SessionService sessionService = new SessionService(hallService);
        TicketService ticketService = new TicketService(sessionService);
        Scanner sc = new Scanner(System.in);

        userService.initAdmin();

        while (true) {
            System.out.println("欢迎进入超级大影院管理系统");
            System.out.println("1. 登录  2. 退出");
            int op = sc.nextInt();
            if (op == 2) break;
            System.out.print("用户名: ");
            String username = sc.next();
            System.out.print("密码: ");
            String password = sc.next();

            User user = userService.login(username, password);
            if (user == null) {
                System.out.println("登录失败！");
                continue;
            }
            switch (user.getType()) {
                case "admin":
                    adminMenu(userService, movieService, sessionService, sc, user);
                    break;
                case "manager":
                    managerMenu(movieService, sessionService, ticketService, sc, user);
                    break;
                case "front":
                    frontMenu(movieService, sessionService, ticketService, sc, user);
                    break;
            }
        }
        sc.close();
    }

    public static void adminMenu(UserService userService, MovieService movieService, SessionService sessionService, Scanner sc, User user) {
        while (true) {
            System.out.println("管理员菜单：1. 密码管理 2. 用户管理 3. 影片管理 4. 场次管理 5. 退出登录");
            int op = sc.nextInt();
            if (op == 5) break;
            switch (op) {
                case 1:
                    userService.passwordMenu(sc, user);
                    break;
                case 2:
                    userService.userManageMenu(sc);
                    break;
                case 3:
                    movieService.movieManageMenu(sc);
                    break;
                case 4:
                    sessionService.sessionManageMenu(sc);
                    break;
            }
        }
    }

    public static void managerMenu(MovieService movieService, SessionService sessionService, TicketService ticketService, Scanner sc, User user) {
        while (true) {
            System.out.println("经理菜单：1. 影片管理 2. 场次管理 3. 售票 4. 退票 5. 退出登录");
            int op = sc.nextInt();
            if (op == 5) break;
            switch (op) {
                case 1:
                    movieService.movieManageMenu(sc);
                    break;
                case 2:
                    sessionService.sessionManageMenu(sc);
                    break;
                case 3:
                    ticketService.buyTicket(sc, user.getUsername());
                    break;
                case 4:
                    ticketService.refundTicket(sc, user.getUsername());
                    break;
            }
        }
    }

    public static void frontMenu(MovieService movieService, SessionService sessionService, TicketService ticketService, Scanner sc, User user) {
        while (true) {
            System.out.println("前台菜单：1. 售票 2. 退票 3. 退出登录");
            int op = sc.nextInt();
            if (op == 3) break;
            switch (op) {
                case 1:
                    ticketService.buyTicket(sc, user.getUsername());
                    break;
                case 2:
                    ticketService.refundTicket(sc, user.getUsername());
                    break;
            }
        }
    }
}